<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Common\Tree; //命名空间，使用Common模块下的Common文件夹下的Tree类
class SortController extends CommonController {
    public function indexAction(){
    	//统计规则数
    	$sql = "select count(id) count from category_test";
    	$count = M()->query($sql);
    	$count = current(M()->query($sql))['count'];

    	// 分页
    	//$Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //$show  = $Page->show();// 分页显示输出

    	$sql = "select * from category_test";

    	$category = M()->query($sql);

		$obj = new Tree();

		$category = $obj->reform($category);   //reform()方法写在Common\Common\Tree类里，传参，把$category 分类表里的数据传给reform()方法处理

	// echo '<pre>';
	// 	print_r($category);
		// $this->assign('show',$show);
		$this->assign('count',$count);
		$this->assign('category',$category);
        $this->display();
    }

    public function addAction() {
    	if (IS_POST) {
    		var_dump($_POST);
    		$fid = $_POST['fid'];
    		$cname = $_POST['cname'];
    		if (empty($cname)) {
    			$this->error('添加失败,请输入分类',U('admin/sort/index'));
    		}
			$cnamesql = "select cname from category_test where cname='{$cname}'";
            $cnameres = M()->query($cnamesql);
            if ($cnameres) {
            	$this->error('添加失败,分类已存在',U('admin/sort/index'));
            }

    		$sql = "insert into category_test (cname,fid) value('{$cname}','{$fid}')";
            $res = M()->execute($sql);
 
            if ($res) {
            	$this->success('添加成功',U('admin/sort/index'));
            }else{
            	$this->error('添加失败',U('admin/sort/index'));
            }
    	}
    }

    public function editAction() {
    	if (IS_POST) {
    		var_dump($_POST);
    		$id = I('post.id');
    		$fid = I('post.fid');
    		$cname = I('post.cname');

    		$cnamesql = "select cname from category_test where cname='{$cname}'";
    		$cnameres = M()->query($cnamesql);
    		if ($cnameres) {
    			$this->error('添加失败,分类已存在',U('admin/sort/index'));
    		}

    		$sql = "update category_test set fid='{$fid}',cname='{$cname}' where id='{$id}'";
    		$res = M()->execute($sql);
    		if ($res) {
    			$this->success('修改成功',U('admin/sort/index'));
    		}else{
    			$this->error('修改失败',U('admin/sort/index'));
    		}
    	}else{
    		$id = I('get.id','');
	    	$sql = "select * from category_test where id = '{$id}'";
	    	$res = current(M()->query($sql));
    	}
    	
		$this->assign('res',$res);
        $this->display();
    }

    public function delAction(){
    	$id = I('get.id');

    	//判断该分级是否有子集，子集的fid对应了该分级的id，如果有子集，有查询结果，那么提示无法删除，先删除子集，没有子集，则直接删除
    	$fidsql = "select * from category_test where fid='{$id}'";
    	$fidres = M()->query($fidsql);
    	var_dump($fidres);

    	if ($fidres) {
    		$this->error('删除失败,该分级有子分级,请先删除子集',U('admin/sort/index'));
    	}

        $sql = "delete from  category_test where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/sort/index'));
        }else{
            $this->error('删除失败',U('admin/sort/index'));
        }
    }

}


